﻿using System.IO;
using System.Xml;
using System.Xml.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using YSUtils.Xml;

namespace UnitTest
{
	/// <summary>
	///This is a test class for XmlIoTest and is intended
	///to contain all XmlIoTest Unit Tests
	///</summary>
	[TestClass]
	public class XmlIoTest
	{
		#region Constants

		private const string FileNameForGet = @"XmlIoTestForGet.xml";
		private const string FileNameForPut = @"XmlIoTestForPut.xml";
		private const string TextXml = @"<?xml version=""1.0"" encoding=""utf-8"" ?><test><element1>Value for element 1</element1></test>";
		#endregion

		#region Fields

		private static string filePathForGet_;
		private static string filePathForPut_;
		#endregion

		#region Properties

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get;
			set;
		}
		#endregion

		#region Additional test attributes

		[ClassInitialize]
		public static void MyClassInitialize(TestContext testContext)
		{
			filePathForGet_ = BuildFilepathForGet();
			filePathForPut_ = BuildFilepathForPut();

			// Create test XML file.
			XElement xml = new XElement("test",
				new XElement("element1",
					new XElement("element2", "value for element 2")));
			xml.Save(filePathForGet_);
		}

		[ClassCleanup]
		public static void MyClassCleanup()
		{
			if (File.Exists(filePathForGet_))
			{
				File.Delete(filePathForGet_);
			}

			if (File.Exists(filePathForPut_))
			{
				File.Delete(filePathForPut_);
			}
		}

		//Use TestInitialize to run code before running each test
		//[TestInitialize()]
		//public void MyTestInitialize()
		//{
		//}
		//
		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup()
		//{
		//}
		//
		#endregion

		#region Methods

		private static string BuildFilepathForGet()
		{
			return Path.Combine(Path.GetTempPath(), FileNameForGet);
		}

		private static string BuildFilepathForPut()
		{
			return Path.Combine(Path.GetTempPath(), FileNameForPut);
		}
		#endregion

		#region Test Methods

		/// <summary>
		///A test for GetXElement
		///</summary>
		[TestMethod]
		public void GetXmlDocuemntTest()
		{
			XmlDocument xmlDocument;

			xmlDocument = XmlIo.GetXmlDocument(filePathForGet_);
			Assert.IsNotNull(xmlDocument, "XmlIo.GetXmlDocument(FileName) failed");

			FileStream fileStream = new FileStream(filePathForGet_, FileMode.Open);
			xmlDocument = XmlIo.GetXmlDocument(fileStream);
			Assert.IsNotNull(xmlDocument, "XmlIo.GetXmlDocument(fileStream) failed");
			fileStream.Close();

			xmlDocument = XmlIo.GetXmlDocumentFromText(TextXml);
			Assert.IsNotNull(xmlDocument, "XmlIo.GetXmlDocumentFromText(TextXml) failed");
		}

		/// <summary>
		/// A test for GetXElement.
		/// </summary>
		[TestMethod]
		public void GetXElementTest()
		{
			XElement xelement;

			xelement = XmlIo.GetXElement(filePathForGet_);
			Assert.IsNotNull(xelement, "XmlIo.GetXElement(FileName) failed");

			FileStream fileStream = new FileStream(filePathForGet_, FileMode.Open);
			xelement = XmlIo.GetXElement(fileStream);
			Assert.IsNotNull(xelement, "XmlIo.GetXElement(fileStream) failed");
			fileStream.Close();

			xelement = XmlIo.GetXElementFromText(TextXml);
			Assert.IsNotNull(xelement, "XmlIo.GetXElementFromText(TextXml) failed");
		}

		/// <summary>
		/// A test for PutXmlDocument.
		/// </summary>
		[TestMethod]
		public void PutXmlDocumentTest()
		{
			XmlDocument srcXmlDocument;
			XmlDocument xmlDocument;

			srcXmlDocument = XmlIo.GetXmlDocument(filePathForGet_);
			Assert.IsNotNull(srcXmlDocument, "XmlIo.GetXmlDocument(FileName) failed");

			XmlIo.PutXmlDocument(srcXmlDocument, filePathForPut_, null);
			xmlDocument = XmlIo.GetXmlDocument(filePathForPut_);
			Assert.IsNotNull(xmlDocument, "XmlIo.GetXmlDocument(FileName) failed");
			Assert.AreEqual(srcXmlDocument.InnerXml, xmlDocument.InnerXml, "Put/Get XmlDocument(fileName) failed");

			FileStream fileStream = new FileStream(filePathForPut_, FileMode.Create);
			XmlIo.PutXmlDocument(srcXmlDocument, fileStream, null);
			fileStream.Flush();
			fileStream.Seek(0, SeekOrigin.Begin);
			xmlDocument = XmlIo.GetXmlDocument(fileStream);
			Assert.IsNotNull(xmlDocument, "XmlIo.GetXmlDocument(FileName) failed");
			Assert.AreEqual(srcXmlDocument.InnerXml, xmlDocument.InnerXml, "Put/Get XmlDocument(fileName) failed");
			fileStream.Close();

			string buffer;
			XmlIo.PutXmlDocumentToText(srcXmlDocument, out buffer);
			xmlDocument = XmlIo.GetXmlDocumentFromText(buffer);
			Assert.IsNotNull(xmlDocument, "XmlIo.GetXmlDocumentFromText(buffer) failed");
			Assert.AreEqual(srcXmlDocument.DocumentElement.InnerXml, xmlDocument.DocumentElement.InnerXml, "PutXmlDocumentToText/GetXmlDocumentFromText(buffer) failed");
		}

		/// <summary>
		/// A test for PutXElement
		/// </summary>
		[TestMethod]
		public void PutXElementTest()
		{
			XElement srcXElement;
			XElement xElement;

			srcXElement = XmlIo.GetXElement(filePathForGet_);
			Assert.IsNotNull(srcXElement, "XmlIo.GetXElement(FileName) failed");

			XmlIo.PutXElement(srcXElement, filePathForPut_, null);
			xElement = XmlIo.GetXElement(filePathForPut_);
			Assert.IsNotNull(xElement, "XmlIo.GetXElement(FileName) failed");
			Assert.AreEqual(srcXElement.ToString(), xElement.ToString(), "Put/Get XElement(fileName) failed");

			FileStream fileStream = new FileStream(filePathForPut_, FileMode.Create);
			XmlIo.PutXElement(srcXElement, fileStream, null);
			fileStream.Flush();
			fileStream.Seek(0, SeekOrigin.Begin);
			xElement = XmlIo.GetXElement(fileStream);
			Assert.IsNotNull(xElement, "XmlIo.GetXmlDocument(FileName) failed");
			Assert.AreEqual(srcXElement.ToString(), xElement.ToString(), "Put/Get XmlDocument(fileName) failed");
			fileStream.Close();

			string buffer;
			XmlIo.PutXElementToText(srcXElement, out buffer);
			xElement = XmlIo.GetXElementFromText(buffer);
			Assert.IsNotNull(xElement, "XmlIo.GetXElementFromText(buffer) failed");
			Assert.AreEqual(srcXElement.ToString(), xElement.ToString(), "Put/Get XmlDocument(fileName) failed");
		}
		#endregion
	}
}
